<template>
  <div class="app-container">
    <h2 style="text-align: center;">发布新课程</h2>

    <el-steps
      :active="1"
      process-status="wait"
      align-center
      style="margin-bottom: 40px;"
    >
      <el-step title="填写课程基本信息" />
      <el-step title="创建课程大纲" />
      <el-step title="提交审核" />
    </el-steps>

    <el-form label-width="120px">
      <el-form-item label="课程标题">
        <el-input
          v-model="courseInfo.title"
          placeholder=" 示例：机器学习项目课：从基础到搭建项目视频课程。专业名称注意大小写"
        />
      </el-form-item>

      <!-- 所属分类 TODO -->

      <!-- 课程讲师 TODO -->
      <el-form-item label="老师">
        <el-select v-model="courseInfo.teacherId" placeholder="请选择">
          <el-option
            v-for="teacher in teacherList"
            :key="teacher.id"
            :label="teacher.name"
            :value="teacher.id"
          />
        </el-select>
      </el-form-item>

      <!-- 一级分类TODO -->
      <el-form-item label="一级课程分类">
        <el-select
          v-model="courseInfo.subjectParentId"
          placeholder="请选择一级分类"
          @change="handleSubjectOneChange"
        >
          <el-option
            v-for="subject in subjectOneList"
            :key="subject.id"
            :label="subject.title"
            :value="subject.id"
          />
        </el-select>

        <!-- 二级分类TODO -->
        <el-select v-model="courseInfo.subjectId" placeholder="请选择二级分类">
          <el-option
            v-for="subject in subjectTwoList"
            :key="subject.id"
            :label="subject.title"
            :value="subject.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="总课时">
        <el-input-number
          :min="0"
          v-model="courseInfo.lessonNum"
          controls-position="right"
          placeholder="请填写课程的总课时数"
        />
      </el-form-item>

      <!-- 课程简介 TODO -->
      <el-form-item label="课程简介">
        <el-input
          v-model="courseInfo.description"
          placeholder="请填写课程的简介"
        />
      </el-form-item>

      <!-- 课程封面 TODO -->
      <el-form-item label="课程封面">
        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :action="actionUrl"
          class="avatar-uploader"
        >
          <!-- 默认的图片，从static中添加 -->
          <img :src="courseInfo.cover" />
        </el-upload>
      </el-form-item>

      <el-form-item label="课程价格">
        <el-input-number
          :min="0"
          v-model="courseInfo.price"
          controls-position="right"
          placeholder="免费课程请设置为0元"
        />
        元
      </el-form-item>

      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="next"
          >保存并下一步</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { addCourseInfo, getListTeacher, getSubjectList } from '@/api/edu/course'

export default {
  data() {
    return {
      actionUrl: process.env.BASE_API + '/eduoss/fileoss',
      teacherList: [],
      subjectAllList: [],
      subjectOneList: [],
      subjectTwoList: [],
      courseInfo: {
        title: '',
        teacherId: '',
        subjectParentId: '',
        subjectId: '',
        lessonNum: 0,
        description: '',
        cover: process.env.DEFAULT_COVER,
        price: 0
      },
      saveBtnDisabled: false // 保存按钮是否禁用
    }
  },

  created() {
    this.getList()
    this.getAllSubjects()
  },

  methods: {
    getList() {
      getListTeacher().then(res => {
        const { items: teacherList } = res.data
        console.log(teacherList)
        this.teacherList = [...teacherList]
      })
    },

    getAllSubjects() {
      getSubjectList().then(res => {
        const { list } = res.data

        this.subjectAllList = list

        // 筛选出一级分类
        this.subjectOneList = list.map(item => ({
          id: item.id,
          title: item.title
        }))
      })
    },

    next() {
      console.log('this.courseInfo ', this.courseInfo)
      addCourseInfo(this.courseInfo).then(res => {
        this.$message({
          type: 'success',
          message: '添加课程信息成功'
        })
        // 这里传入已经添加的课程的id
        this.$router.push({ path: `/course/chapter/${res.data.courseId}` })
      })
    },

    handleSubjectOneChange(e) {
      this.courseInfo.subjectId = ''

      // 筛选出二级分类
      this.subjectAllList.forEach(item => {
        if (item.id === e) this.subjectTwoList = [...item.children]
      })
    },

    /**
     * 成功上传封面
     */
    handleAvatarSuccess(res, file) {
      console.log(res) // 上传响应
      console.log(URL.createObjectURL(file.raw)) // base64编码
      this.courseInfo.cover = res.data.url
    },

    /**
     * 上传之前调用的方法
     * 限制文件类型、大小。
     */
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>

<style lang="scss"></style>
